package cn.e2win.biz.activity;

import java.sql.SQLException;
import java.util.Map;

import cn.e2win.base.entity.SysUser;
import cn.e2win.biz.security.UserSession;
import cn.e2win.biz.util.DateUtils;
import cn.e2win.biz.util.StringUtils;
import org.springframework.stereotype.Repository;

import cn.e2win.base.entity.BaseEntity;
import cn.e2win.base.json.vo.Pager;
import cn.e2win.biz.base.BaseDAOHibernateImpl;
import cn.e2win.biz.base.CommonStatic;
import cn.e2win.biz.dbo.Activity;


@Repository
public class ActivityDaoImpl extends BaseDAOHibernateImpl implements ActivityDao {

	String entitySql_Activity = "SELECT * FROM ("
			
			+ " SELECT t0.*"
			
			+ " , CASE WHEN SYSDATE()<t0.beginTime THEN 'waitting' "
			+ " WHEN SYSDATE()>t0.beginTime AND SYSDATE()<t0.endTime THEN 'running' "
			+ " WHEN SYSDATE()>t0.endTime THEN 'evaluate' "
			+ " WHEN 1=2 THEN 'evaluated' ELSE 'unknow' END AS processStatus "
			
			+ " , t1.label AS activityTag "

			+ " , t2.dsEmpIds AS dsEmpIds "
			+ " , t2.dsEmpNames AS dsEmpNames "
			+ " , t3.guestNames AS guestNames "
			
			+ " FROM activity t0 "
			+ " LEFT JOIN ("+CommonStatic.labSql+") AS t1 ON t0.id = t1.entityId AND t1.entityType='Activity' AND t1.labelType='activityTag'"
			+ " LEFT JOIN ("+CommonStatic.dsRelationSql+") AS t2 ON t0.id = t2.entityId AND t2.entityType='Activity'"
			+ " LEFT JOIN ("+CommonStatic.guestRelationSql + ") AS t3 ON t0.id = t3.entityId AND t3.entityType='Activity'"
			
			+ ") t "
			+ " WHERE 1=1 ";

	
	@Override
	public void pagerActivity(Pager pager) {
		try {
			String sql = entitySql_Activity;
			String user_I = pager.getScMap().get("user_I");
			if(!StringUtils.isEmpty(user_I)){
				SysUser user = pager.getCurrUser();
				if(user_I.equals("addBy") || user_I.equals("master")){
					sql += " And createById="+user.getId()+" ";
				}else if(user_I.equals("admin")){
					sql += " And (adminUserId="+user.getId()+" or adminUserId2 ="+user.getId()+") ";
				}else if(user_I.equals("join")){
					sql += " And dsEmpNames like '%"+user.getUserName()+"%' ";
				}
			}
			
			if("TODAY".equals(pager.getScMap().get("activityDate_I"))){
				sql += " And t.beginTime < STR_TO_DATE('"+DateUtils.getCustomDayE(0) + "','%Y-%m-%d %H:%i:%s') "
						+ " And t.beginTime > STR_TO_DATE('"+DateUtils.getCustomDayB(0)+ "','%Y-%m-%d %H:%i:%s') ";
			}else if("TOMORROW".equals(pager.getScMap().get("activityDate_I"))){
				sql += " And t.beginTime > STR_TO_DATE('"+DateUtils.getCustomDayB(0)+ "','%Y-%m-%d %H:%i:%s') "
						+ " And t.beginTime < STR_TO_DATE('"+DateUtils.getCustomDayE(3)+ "','%Y-%m-%d %H:%i:%s') ";
			}else if("SATURDAY".equals(pager.getScMap().get("activityDate_I"))){
				sql += " And t.beginTime > STR_TO_DATE('"+DateUtils.getCustomDayE(3)+ "','%Y-%m-%d %H:%i:%s') ";
			}
			
			String adminUserId = pager.getScMap().get("adminUserId_I");
			if(!StringUtils.isEmpty(adminUserId)){
				sql += "And (adminUserId="+adminUserId+" or adminUserId2="+adminUserId+") ";
			}
			//用于搜索
			String title_I = pager.getScMap().get("title_I");
			if(!StringUtils.isEmpty(title_I)){
				sql += " And (activityName like '%"+title_I+"%' or address like '%"+title_I+"%' ) ";
			}
			//导师
			String dsEmpIds_I = pager.getScMap().get("dsEmpIds_I");
			if(!StringUtils.isEmpty(dsEmpIds_I)){
				sql += " And (dsEmpIds like '%|"+dsEmpIds_I+"|%') ";
			}
			pager.processSql(sql + Pager.DEF_ORDER_BY);
			processPager(pager);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Map<String, Object>getEntity(BaseEntity entity){
		if(entity instanceof Activity){
			return super.get1stRecordBySql(entitySql_Activity + " AND id=" + entity.getId(), null);
		}else{
			return super.getEntity(entity);
		}
	}
	

	@Override
	public Map<String, Object>getCyzjOrgDetail(Integer id, Map<String, String> param){
		String sql = "SELECT * FROM ("
				
				+ " SELECT t0.*"
				
				+ " , CASE WHEN SYSDATE()<t0.beginTime THEN 'waitting' "
				+ " WHEN SYSDATE()>t0.beginTime AND SYSDATE()<t0.endTime THEN 'running' "
				+ " WHEN SYSDATE()>t0.endTime THEN 'evaluate' "
				+ " WHEN 1=2 THEN 'evaluated' ELSE 'unknow' END AS processStatus "
				
				+ " , t1.label AS activityTag "

				+ " , t2.dsEmpIds AS dsEmpIds "
				+ " , t2.dsEmpNames AS dsEmpNames "
				+ " , t3.guestNames AS guestNames "
				+  CommonStatic.getDistanceSql(param.get("x_I"), param.get("y_I"), " t0.x", " t0.y", "distance", "distanceKm") 
				
				+ " FROM activity t0 "
				+ " LEFT JOIN ("+CommonStatic.labSql+") AS t1 ON t0.id = t1.entityId AND t1.entityType='Activity' AND t1.labelType='activityTag'"
				+ " LEFT JOIN ("+CommonStatic.dsRelationSql+") AS t2 ON t0.id = t2.entityId AND t2.entityType='Activity'"
				+ " LEFT JOIN ("+CommonStatic.guestRelationSql + ") AS t3 ON t0.id = t3.entityId AND t3.entityType='Activity'"
				
				+ ") t "
				+ " WHERE 1=1 "
				+ " AND id=" + id;
		return super.get1stRecordBySql(sql , param);
	}

}
